import {createStore} from "vuex";
import {constantRoutes,loadView} from '@/router'
import router from '@/router'

import {getRouters} from "@/api/base/menu"
import store from '@/store'
import {getLocalStoreObj, setLocalStoreObj, setLocalStoreString,getLocalStoreString,removeLocalStore} from "@/utils/hongUtils";


const permission = {
    namespaces:true,
    state: {
        defaultRouters: [],//默认路由
        defaultMenu: [],//默认菜单-左边菜单栏
        defaultSystem: [],//默认系统-右边系统选项
        defaultSelectSystem:{},//默认选中的系统
        interview:[   {
            path:'/index',
            name:'首页'
        }],//选中界面数组 {path:'',name:''}
        currentShow:'/index',//当前选中 默认首页
    },
    getters: {},
    mutations: {
        //设置默认路由
        SET_DEFAULT_ROUTES: (state, data) => {
            state.defaultRouters = data
            // state.routes = constantRoutes.concat(data)
        },
        //默认菜单
        SET_DEFAULT_MENU: (state, data) => {
            state.defaultMenu = data
        },
        //默认系统
        SET_DEFAULT_SYSTEM: (state, data) => {
            state.defaultSystem = data
        },
        //默认选中的系统
        SET_DEFAULT_SELECT_SYSTEM: (state, data) => {
            state.defaultSelectSystem = data
            //选中的子菜单
            if (data && data.children && data.children.length > 0) {
                state.defaultMenu = data.children
            }
        },
        //访问过的历史
        SET_INTERVIEW: (state, data) => {
            state.interview = data
        },
        //移除一个的历史
        REMOVE_ONE_INTERVIEW: (state, data) => {
            let d = state.interview.filter(o=>{return o.path != data});
            state.interview = d;
            state.currentShow = d[d.length-1].path
            setLocalStoreObj('interview',state.interview);//刷新页签
            setLocalStoreString('currentShow',state.currentShow);//刷新选中
        },
        //清除全部历史
        REMOVE_ALL_INTERVIEW: (state, data) => {
            console.log("清楚全部历史")
            state.interview = [   {
                path:'/index',
                name:'首页'
            }];
            setLocalStoreObj('interview',state.interview);//刷新页签
            state.currentShow = '/index'
        },
        //当前选中
        SET_CURRENT_SHOW: (state, data) => {
            state.currentShow = data
        },
        //跳转 {path:'',name:''}
        ADD_CURRENT_SHOW: (state, data) => {
            //选中
            state.currentShow = data.path
            setLocalStoreString('currentShow',state.currentShow);//刷新选中
            if(state.interview.length > 0){
                //是否在数组里面
                let d = state.interview.filter(o=>{return o.path == data.path});
                if(d.length >0){
                    return;
                }
            }
            //数组添加
            state.interview.push(data);
            setLocalStoreObj('interview',state.interview);//刷新页签
        },

    },
    actions: {
        GenerateRoutes( { commit }){
            return new Promise((resolve, reject) => {
                getRouters().then(res=>{
                    let data = res.data;
                    dealRouter(data);
                    let select;//跳转
                    let xzSystem = data[0];//默认选中系统 自动设置左侧菜单
                    let system = data;//右侧列表菜单
                    commit("SET_DEFAULT_ROUTES",xzSystem);//所有路由信息
                    commit("SET_DEFAULT_SYSTEM",system);//右侧列表菜单
                    commit("SET_DEFAULT_SELECT_SYSTEM",xzSystem);//选中系统
                    let interVal = getLocalStoreObj('interview');//恢复页签
                    if (interVal){
                        commit("SET_INTERVIEW",interVal);//恢复页签
                        select = getLocalStoreString('currentShow');//获取选中
                        commit("SET_CURRENT_SHOW",select);//恢复选中
                    }
                    resolve(select);
                }).catch(e=>{
                    console.error("路由错误",e);
                    reject();
                })
            })

        }
    },
    modules: {}
}
export default permission

/**
 * 注册路由-注册到path为  /  下
 * @param routers
 */
export function dealRouter(routers){
    const f = function deal(data,path,arr){
        //无子集设置当前
        //跳转path
        data.path = path?(path + "/" + data.path):('/' +data.path);
        //动态路由注册
        if(data.lx == 1){
            if (data.component) {
                data.component = loadView(data.component);
                arr.push(data)
            }
        }

        //有子集设置子集
        if (data && data.children && data.children.length) {
            let len = data.children.length;
            for (let i = 0; i < len; i++) {
                let  route = data.children[i];
                deal(route,data.path,arr);
            }
        }
        return;
    }
    //处理
    let len = routers.length;
    let addR = [];//添加到默认数组里面的值
    for (let i = 0; i < len; i++) {
        let  route = routers[i];
        f(route,null,addR);
    }
    addR.forEach(o=>router.addRoute('layout',  o))
}
// const modulesFiles = import.meta.glob('./modules/**/*.js')
